home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / blender.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  4KB  |  162 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* blender renders two spinning icosahedrons (red and green).
  9.    The blending factors for the two icosahedrons vary sinusoidally
  10.    and slightly out of phase.  blender also renders two lines of
  11.    text in a stroke font: one line antialiased, the other not.  */
  12.  
  13. #include <GL/glut.h>
  14. #include <stdio.h>
  15. #include <math.h>
  16.  
  17. GLfloat light0_ambient[] =
  18. {0.2, 0.2, 0.2, 1.0};
  19. GLfloat light0_diffuse[] =
  20. {0.0, 0.0, 0.0, 1.0};
  21. GLfloat light1_diffuse[] =
  22. {1.0, 0.0, 0.0, 1.0};
  23. GLfloat light1_position[] =
  24. {1.0, 1.0, 1.0, 0.0};
  25. GLfloat light2_diffuse[] =
  26. {0.0, 1.0, 0.0, 1.0};
  27. GLfloat light2_position[] =
  28. {-1.0, -1.0, 1.0, 0.0};
  29. float s = 0.0;
  30. GLfloat angle1 = 0.0, angle2 = 0.0;
  31.  
  32. void 
  33. output(GLfloat x, GLfloat y, char *text)
  34. {
  35.   char *p;
  36.  
  37.   glPushMatrix();
  38.   glTranslatef(x, y, 0);
  39.   for (p = text; *p; p++)
  40.     glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  41.   glPopMatrix();
  42. }
  43.  
  44. void 
  45. display(void)
  46. {
  47.   static GLfloat amb[] =
  48.   {0.4, 0.4, 0.4, 0.0};
  49.   static GLfloat dif[] =
  50.   {1.0, 1.0, 1.0, 0.0};
  51.  
  52.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  53.   glEnable(GL_LIGHT1);
  54.   glDisable(GL_LIGHT2);
  55.   amb[3] = dif[3] = cos(s) / 2.0 + 0.5;
  56.   glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
  57.   glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
  58.  
  59.   glPushMatrix();
  60.   glTranslatef(-0.3, -0.3, 0.0);
  61.   glRotatef(angle1, 1.0, 5.0, 0.0);
  62.   glCallList(1);        /* render ico display list */
  63.   glPopMatrix();
  64.  
  65.   glClear(GL_DEPTH_BUFFER_BIT);
  66.   glEnable(GL_LIGHT2);
  67.   glDisable(GL_LIGHT1);
  68.   amb[3] = dif[3] = 0.5 - cos(s * .95) / 2.0;
  69.   glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
  70.   glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
  71.  
  72.   glPushMatrix();
  73.   glTranslatef(0.3, 0.3, 0.0);
  74.   glRotatef(angle2, 1.0, 0.0, 5.0);
  75.   glCallList(1);        /* render ico display list */
  76.   glPopMatrix();
  77.  
  78.   glPushAttrib(GL_ENABLE_BIT);
  79.   glDisable(GL_DEPTH_TEST);
  80.   glDisable(GL_LIGHTING);
  81.   glMatrixMode(GL_PROJECTION);
  82.   glPushMatrix();
  83.   glLoadIdentity();
  84.   gluOrtho2D(0, 1500, 0, 1500);
  85.   glMatrixMode(GL_MODELVIEW);
  86.   glPushMatrix();
  87.   glLoadIdentity();
  88.   /* Rotate text slightly to help show jaggies. */
  89.   glRotatef(4, 0.0, 0.0, 1.0);
  90.   output(200, 225, "This is antialiased.");
  91.   glDisable(GL_LINE_SMOOTH);
  92.   glDisable(GL_BLEND);
  93.   output(160, 100, "This text is not.");
  94.   glPopMatrix();
  95.   glMatrixMode(GL_PROJECTION);
  96.   glPopMatrix();
  97.   glPopAttrib();
  98.   glMatrixMode(GL_MODELVIEW);
  99.  
  100.   glutSwapBuffers();
  101. }
  102.  
  103. void 
  104. idle(void)
  105. {
  106.   angle1 = (GLfloat) fmod(angle1 + 0.8, 360.0);
  107.   angle2 = (GLfloat) fmod(angle2 + 1.1, 360.0);
  108.   s += 0.05;
  109.   glutPostRedisplay();
  110. }
  111.  
  112. void 
  113. visible(int vis)
  114. {
  115.   if (vis == GLUT_VISIBLE)
  116.     glutIdleFunc(idle);
  117.   else
  118.     glutIdleFunc(NULL);
  119. }
  120.  
  121. int 
  122. main(int argc, char **argv)
  123. {
  124.   glutInit(&argc, argv);
  125.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  126.   glutCreateWindow("blender");
  127.   glutDisplayFunc(display);
  128.   glutVisibilityFunc(visible);
  129.  
  130.   glNewList(1, GL_COMPILE);  /* create ico display list */
  131.   glutSolidIcosahedron();
  132.   glEndList();
  133.  
  134.   glEnable(GL_LIGHTING);
  135.   glEnable(GL_LIGHT0);
  136.   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  137.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  138.   glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
  139.   glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  140.   glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
  141.   glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
  142.   glEnable(GL_DEPTH_TEST);
  143.   glEnable(GL_CULL_FACE);
  144.   glEnable(GL_BLEND);
  145.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  146.   glEnable(GL_LINE_SMOOTH);
  147.   glLineWidth(2.0);
  148.  
  149.   glMatrixMode(GL_PROJECTION);
  150.   gluPerspective( /* field of view in degree */ 40.0,
  151.   /* aspect ratio */ 1.0,
  152.     /* Z near */ 1.0, /* Z far */ 10.0);
  153.   glMatrixMode(GL_MODELVIEW);
  154.   gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
  155.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  156.     0.0, 1.0, 0.);      /* up is in positive Y direction */
  157.   glTranslatef(0.0, 0.6, -1.0);
  158.  
  159.   glutMainLoop();
  160.   return 0;             /* ANSI C requires main to return int. */
  161. }
  162.